<template>
  <div id="system_member">
    <!--顶部搜索框内容开始-->
    <div class="role_seek">
      <el-select v-model="forms.statuss" placeholder="请选择用户状态">
        <el-option
          v-for="item in status"
          :key="item.staData"
          :label="item.staName"
          :value="item.staData"
        >
        </el-option>
      </el-select>

      <el-select v-model="forms.levels" placeholder="请选择用户等级">
        <el-option
          v-for="item in level"
          :key="item.levData"
          :label="item.levName"
          :value="item.levData"
        >
        </el-option>
      </el-select>

      <el-select v-model="forms.genders" placeholder="请选择用户性别">
        <el-option
          v-for="item in gender"
          :key="item.genData"
          :label="item.genName"
          :value="item.genData"
        >
        </el-option>
      </el-select>

      <el-input
        class="role_input"
        placeholder="请输入用户Id"
        v-model="forms.inputID"
        clearable
        @change="seek"
      ></el-input>
      <el-input
        class="role_input"
        placeholder="请输入昵称"
        v-model="forms.inputName"
        clearable
        @change="seek"
      ></el-input>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="seek"  v-power="'system:user:query'"
        >查找</el-button
      >

      <!--修改添加弹出框开始-->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          class="pop-up"
          status-icon
          label-position="left"
        >
          <el-form-item
            prop="nickname"
            label="用户昵称"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item
            prop="phone"
            label="用户手机号"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item
            prop="password"
            label="用户密码"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.password" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item
            prop="gender"
            label="用户性别"
            :label-width="formLabelWidth"
          >
            <el-select v-model="form.gender" placeholder="请选择用户性别">
              <el-option
                v-for="item in gender"
                :key="item.genData"
                :label="item.genName"
                :value="item.genData"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item
            prop="level"
            label="用户等级"
            :label-width="formLabelWidth"
          >
            <el-select v-model="form.level" placeholder="请选择用户性别">
              <el-option
                v-for="item in level"
                :key="item.levData"
                :label="item.levName"
                :value="item.levData"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item
            prop="status"
            label="用户状态"
            :label-width="formLabelWidth"
          >
            <el-select v-model="form.status" placeholder="请选择用户性别">
              <el-option
                v-for="item in status"
                :key="item.staData"
                :label="item.staName"
                :value="item.staData"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelbutton('form')">取 消</el-button>
          <el-button
            type="primary"
            @click="bool ? adduser('form') : compile('form')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!--修改添加弹出框结束-->
    </div>
    <!--顶部搜索框内容结束-->

    <!--底部列表内容部分-->
    <template>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column
          prop="id"
          label="用户ID"
          sortable
          align="center"
        ></el-table-column>
        <el-table-column
          prop="nickname"
          label="昵称"
          align="center"
        ></el-table-column>
        <el-table-column
          label="手机号码"
          prop="phone"
          align="center"
        ></el-table-column>
        <el-table-column prop="gender" label="性别" align="center">
          <template slot-scope="scope">
            <el-tag> {{ scope.row.gender | genderMe }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="level" label="会员等级" align="center">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.level | levelMe }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="生日" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.gmtCreate | filterTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag> {{ scope.row.status | statuMe }} </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="lastLoginIp" label="上次登录IP" align="center">
        </el-table-column>
        <el-table-column label="上次登录时间" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.gmtLastLogin | filterTime }}</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="address"
          label="操作"
          width="300px"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              class="button_redact"
              size="mini"
              v-power="'admin.user.updateStatus'"
              @click="state(scope.$index, scope.row)"
            >
              {{ !scope.row.status | statuMe }}
            </el-button>
            <el-button
              class="button_redact"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
               v-power="'system:user:update'"
              >编辑</el-button
            >
            <el-button
              class="button_remove"
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
               v-power="'system:user:delete'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--底部列表内容部分结束-->

    <!--底部分页开始-->
    <div class="block">
      <Paging :forms="forms" :gain="gain"></Paging
      ><!--分页封装-->
    </div>
    <!--底部分页结束-->
  </div>
</template>

<script>
import Member from "@/api/system/Member";
import Paging from "../../components/Paging.vue"; //自定义组件
export default {
  data() {
    //验证角色名称是否为空
    var nickname = (rule, value, callback) => {
      // console.log(value);
      if (value === "" || value === undefined) {
        //非空验证
        callback(new Error("角色名称不能为空"));
      } else {
        callback();
      }
    };
    //验证密码是否为空
    var password = (rule, value, callback) => {
      let pwds = /^\S{6,}$/;
      if (value === "" || value === undefined) {
        //非空验证
        callback(new Error("密码不能为空"));
      } else if (!pwds.test(value)) {
        callback(new Error("请输入6-18个字母或数字"));
      } else {
        callback();
      }
    };
    //验证手机号
    var phone = (rule, value, callback) => {
      let numbers = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (value === "" || value === undefined) {
        //非空验证
        callback(new Error("手机号不能为空"));
      } else if (!numbers.test(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };

    return {
      //弹出框
      tableData: [{}], //table里面的数据
      dialogFormVisible: false, //控制弹出框是否显示
      formLabelWidth: "120px", //弹出框宽度
      title: "创建", //弹出框标题
      bool: true, //布尔值 判断编辑还是修改 为true是创建 否则就是修改

      //分页

      level: [
        { levData: 0, levName: "普通会员" },
        { levData: 1, levName: "VIP会员" },
        { levData: null, levName: "全部" }
      ], //用户等级
      gender: [
        { genData: 2, genName: "男" },
        { genData: 1, genName: "女" },
        { genData: null, genName: "全部" }
      ], //用户性别
      status: [
        { staData: 0, staName: "激活" },
        { staData: 1, staName: "冻结" },
        { staData: null, staName: "全部" }
      ],
      //输入框数值
      form: {
        nickname: "", //用户昵称
        password: "", //弹出框输入的密码
        phone: "", //手机号
        id: "", //会员id
        level: "", //会员等级
        gender: "", //会员性别
        status: "" //会员状态
      },
      forms: {
        pageSize: 10, //limit
        total: 5, //分页——-共几条
        pageNo: 1, //页码
        levels: "",
        genders: "",
        statuss: "",
        inputID: "", //index框输入内容
        inputName: "" //index框输入内容
      },
      //弹出框验证
      rules: {
        nickname: [{ required: true, validator: nickname, trigger: "blur" }], //如果管理员名称验证是正确的就给蓝色
        password: [{ required: true, validator: password, trigger: "blur" }], //如果密码验证是正确的就给蓝色
        phone: [{ required: true, validator: phone, trigger: "blur" }], //如果手机号验证是正确的就给蓝色
        level: [{ required: true, trigger: "blur" }], //如果会员等级验证是正确的就给蓝色
        gender: [{ required: true, trigger: "blur" }], //如果性别验证是正确的就给蓝色
        status: [{ required: true, trigger: "blur" }] //如果验证是正确的就给蓝色
      },
      //点击当前的下标
      index: "", //当前点击的下标
      id: "", //点击当前的id
      roleId: [{}] //管理员角色id
    };
  },
  components: {
    //注册组件
    Paging
  },
  created() {
    this.gain();
  },
  filters: {
    //激活冻结状态
    statuMe(data) {
      if (data == 1) {
        return "激活";
      } else {
        return "冻结";
      }
    },
    //会员等级
    levelMe(data) {
      if (data == 1) {
        return "vip会员";
      } else {
        return "普通会员";
      }
    },
    //性别
    genderMe(data) {
      if (data == -1) {
        return "未知性别";
      } else if (data == 1) {
        return "女";
      } else {
        return "男";
      }
    }
  },
  methods: {
    //用户当前状态
    state(index, row) {
      Member.state(row.id, row.status ? 0 : 1).then(res => {
        if (res.data.errmsg == "成功") {
          this.Prosperity.Messages("状态更改成功", "success");
          this.tableData[index].status = row.status ? 0 : 1; //实现页面更新效果
        } else {
          this.Prosperity.Messages(res.data.errmsg, "error");
        }
      });
    },
    //点击取消按钮
    cancelbutton() {
      this.dialogFormVisible = false;
    },
    //获取列表数据内容
    gain() {
      var _this = this;
      Member.gain(_this.forms).then(res => {
        console.log(res);
        _this.tableData = res.data.data.items; //将接口列表内容赋值给列表数组
        _this.forms.pageSize = res.data.data.pageSize; //页码长度
        _this.forms.pageNo = res.data.data.pageNo; //页码
        _this.forms.total = res.data.data.total;
      });
    },
    //点击编辑按钮触发
    handleEdit(index, row) {
      console.log(this.forms);
      //清空输入框下面的提示（输入错误）
      this.$nextTick(() => {
        if (this.$refs.form) {
          this.$refs.form.resetFields();
        }
      });

      this.index = index; //点击时的row下标

      //获取到数据在点击进去时赋值入输入框
      this.form = row;
      this.bool = false; //改变bool 触发编辑接口
      this.title = "编辑"; //改变弹出框标题
      this.dialogFormVisible = true; //显示弹出框
    },

    //编辑角色接口
    compile(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          var _this = this;
          Member.compile(_this.form).then(res => {
            console.log(res, this.forms);
            //弹框  如果用户名或者密码等于空的话就弹出
            if (res.data.errmsg == "成功") {
              //使用数据再次更新页面
              _this.gain();
              //限制弹出框数量
              this.Prosperity.Messages("编辑角色成功", "success");
              this.dialogFormVisible = false;
            } else {
              this.Prosperity.Messages(res.data.errmsg, "error");
              this.dialogFormVisible = false;
            }
          });
          this.dialogFormVisible = false;
          this.cancelbutton();
        } else {
          return false;
        }
      });
    },
    //搜索信息
    seek() {
      this.forms.pageNo = 1; //在最后一页搜索时回到第一页
      this.gain();
    },

    //点击删除当前内容
    handleDelete(index, row) {
      this.index = index; //当前删除行的id
      this.form.id = row.id; //当前删除行的id
      this.form.nickname = row.nickname; //当前删除行的会员名
      this.$confirm(
        "此操作将永久删除该角色---" + row.nickname + "---, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          //确定删除的话
          this.cancel();
        })
        .catch(() => {
          this.Prosperity.Messages("已取消删除");
        });
    },
    //删除管理员内容接口
    cancel() {
      var _this = this;
      Member.cancel(_this.form)
        .then(res => {
          if (res.data.errmsg == "成功") {
            this.tableData.splice(this.index, 1);
            this.Prosperity.Messages("删除角色成功", "success");
            _this.dialogFormVisible = false;
          } else {
            this.Prosperity.Messages("删除角色失败", "errmsg");
            _this.dialogFormVisible = false;
          }
        })
        .catch(err => {
          return err;
        });
    }
  }
};
</script>

<style scoped="scoped">
/*最大父盒子开始*/
#system_member {
  padding: 20px;
}
/*最大父盒子结束*/

/*顶部index框开始*/
.el-select {
  margin-right: 5px;
}
.role_seek > .el-input {
  width: 200px !important;
  height: 40px !important;
  margin-right: 5px;
}
.role_seek > .el-input > .el-input__inner {
  font-size: inherit;
  width: 100% !important;
  height: 100% !important;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

/*顶部index框结束*/

/*顶部按钮开始*/
.role_seek > .el-button--primary {
  line-height: 1px;
  font-size: 14px;
  border: 1px solid #409eff !important;
  background: #409eff !important;
  color: #fff;
  vertical-align: middle;
  padding: 12px 20px;
  margin-left: 5px;
}
/*顶部按钮结束*/

/*底部列表内容*/

.el-table {
  margin-top: 23px;
}
.el-table td {
  text-align: center !important;
}
.cell {
  font-size: 12px;
  padding: 0px 10px;
}
.el-table__row > td {
  padding: 8px 0px;
}
.button_redact {
  background: #409eff !important;
  color: #fff !important;
  border: 1px solid #409eff;
}
/*编辑内容按钮*/
.button_redact:hover {
  background: #66b1ff !important;
  border: 1px solid #66b1ff;
}
.button_remove {
  background: #f56c6c !important;
  color: #fff !important;
  border: #f56c6c 1px solid;
}
.button_remove:hover {
  background: #f78989 !important;
  border: 1px solid #f78989;
}
.el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden;
}
/*底部列表内容*/

/*底部分页*/
.block {
  margin: 55px 0px;
}

.el-select .el-input .el-select__caret {
  position: absolute;
  right: 150px;
}
/*底部分页结束*/

/*添加弹出框*/

.el-input__inner {
  text-indent: 5px;
}
.el-form-item {
  text-align: none !important;
}
.el-input__suffix {
  left: none !important;
}
/*添加弹出框结束*/
</style>
